package com.wetao.module_homework.presenter;

import android.content.Context;

import com.google.gson.Gson;
import com.wetao.lib_common.base.BaseListResponse;
import com.wetao.lib_common.base.BaseResponse;
import com.wetao.lib_common.bean.response.CommitAssistantAnswerResp;
import com.wetao.lib_common.bean.response.GetAssistantQuestionList;
import com.wetao.lib_common.bean.response.GetSubjectExamNumber;
import com.wetao.lib_common.bean.response.StudentExamListInfo;
import com.wetao.lib_common.net.RetryWithDelay;
import com.wetao.lib_common.net.RxUtils;
import com.wetao.lib_common.net.ServerUtils;
import com.wetao.lib_common.net.callback.RequestCallback;
import com.wetao.lib_common.net.callback.RxErrorHandler;
import com.wetao.lib_common.utils.ToastUtil;
import com.wetao.module_homework.contract.HomeworkCodeActivityCon;
import com.wetao.module_homework.contract.HomeworkMainFragmentCon;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import okhttp3.RequestBody;


/**
 * author: zhouyh
 * created on: 2019-11-15 16:33
 * description:
 */
public class HomeworkCodeActivityPre extends HomeworkCodeActivityCon.Presenter {


    private Context mContext;

    public HomeworkCodeActivityPre(Context mContext) {
        this.mContext = mContext;
    }

    @Override
    public void getAssistantQuestionList(HashMap<String, String> map, boolean isDialog, boolean cancelable) {
        if (getView()==null)return;
        String json =(new Gson()).toJson(map);
        RequestBody requestBody =  RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),json);
        ServerUtils.getCommonApi().getAssistantQuestionList(requestBody)
                .retryWhen(new RetryWithDelay(3,2))
                .compose(RxUtils.<BaseListResponse<GetAssistantQuestionList>>bindToLifecycle(getView()))
                .compose(RxUtils.<BaseListResponse<GetAssistantQuestionList>>getSchedulerTransformer())
                .subscribe(new RequestCallback<BaseListResponse<GetAssistantQuestionList>>(mContext, RxErrorHandler.getInstance(),isDialog,cancelable) {
                    @Override
                    public void onNext(BaseListResponse<GetAssistantQuestionList> baseResponse) {
                        super.onNext(baseResponse);
                        if(baseResponse.getCode() == 0){
                            if (getView()!=null){
                                getView().getAssistantQuestionListResult(baseResponse);
                            }
                        }else{
                            ToastUtil.initToast(baseResponse.getMsg());
                            if (getView()!=null){
                                getView().getAssistantQuestionListFailed();
                            }
                        }
                    }
                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        ToastUtil.initToast(e.getMessage());
                        if (getView()!=null){
                            getView().getAssistantQuestionListFailed();
                        }
                    }
                });
    }

    @Override
    public void getCourseQuestionList(HashMap<String, String> map, boolean isDialog, boolean cancelable) {
        if (getView()==null)return;
        String json =(new Gson()).toJson(map);
        RequestBody requestBody =  RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),json);
        ServerUtils.getCommonApi().getCourseQuestionList(requestBody)
                .retryWhen(new RetryWithDelay(3,2))
                .compose(RxUtils.<BaseResponse<GetAssistantQuestionList>>bindToLifecycle(getView()))
                .compose(RxUtils.<BaseResponse<GetAssistantQuestionList>>getSchedulerTransformer())
                .subscribe(new RequestCallback<BaseResponse<GetAssistantQuestionList>>(mContext, RxErrorHandler.getInstance(),isDialog,cancelable) {
                    @Override
                    public void onNext(BaseResponse<GetAssistantQuestionList> baseResponse) {
                        super.onNext(baseResponse);
                        if(baseResponse.getCode() == 0){
                            BaseListResponse<GetAssistantQuestionList> resp = new BaseListResponse<>();
                            resp.setCode(baseResponse.getCode());
                            resp.setMsg(baseResponse.getMsg());
                            List<GetAssistantQuestionList> d = new ArrayList<>();
                            d.add(baseResponse.getData());
                            resp.setData(d);
                            if (getView()!=null){
                                getView().getAssistantQuestionListResult(resp);
                            }
                        }else{
                            ToastUtil.initToast(baseResponse.getMsg());
                            if (getView()!=null){
                                getView().getAssistantQuestionListFailed();
                            }
                        }
                    }
                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        ToastUtil.initToast(e.getMessage());
                        if (getView()!=null){
                            getView().getAssistantQuestionListFailed();
                        }
                    }
                });
    }

    @Override
    public void commitAssistantAnswer(String json, boolean isDialog, boolean cancelable) {
        if (getView()==null)return;
        RequestBody requestBody =  RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),json);
        ServerUtils.getCommonApi().commitAssistantAnswer(requestBody)
                .retryWhen(new RetryWithDelay(3,2))
                .compose(RxUtils.<BaseResponse<CommitAssistantAnswerResp>>bindToLifecycle(getView()))
                .compose(RxUtils.<BaseResponse<CommitAssistantAnswerResp>>getSchedulerTransformer())
                .subscribe(new RequestCallback<BaseResponse<CommitAssistantAnswerResp>>(mContext, RxErrorHandler.getInstance(),isDialog,cancelable) {
                    @Override
                    public void onNext(BaseResponse<CommitAssistantAnswerResp> baseResponse) {
                        super.onNext(baseResponse);
                        if(baseResponse.getCode() == 0){
                            if (getView()!=null){
                                getView().commitAssistantAnswerResult(baseResponse);
                            }
                        }else{
                            ToastUtil.initToast(baseResponse.getMsg());
                            if (getView()!=null){
                                getView().commitAssistantAnswerFailed(baseResponse.getCode());
                            }
                        }
                    }
                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        ToastUtil.initToast(e.getMessage());
                        if (getView()!=null){
                            getView().commitAssistantAnswerFailed(-1);
                        }
                    }
                });
    }

    @Override
    public void queryClassroomTestQuestionList(HashMap<String, String> map, boolean isDialog, boolean cancelable) {
        if (getView()==null)return;
        String json =(new Gson()).toJson(map);
        RequestBody requestBody =  RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),json);
        ServerUtils.getCommonApi().queryClassroomTestQuestionList(requestBody)
                .retryWhen(new RetryWithDelay(3,2))
                .compose(RxUtils.<BaseResponse<GetAssistantQuestionList>>bindToLifecycle(getView()))
                .compose(RxUtils.<BaseResponse<GetAssistantQuestionList>>getSchedulerTransformer())
                .subscribe(new RequestCallback<BaseResponse<GetAssistantQuestionList>>(mContext, RxErrorHandler.getInstance(),isDialog,cancelable) {
                    @Override
                    public void onNext(BaseResponse<GetAssistantQuestionList> baseResponse) {
                        super.onNext(baseResponse);
                        if(baseResponse.getCode() == 0){
                            if (getView()!=null){
                                getView().queryClassroomTestQuestionListResult(baseResponse);
                            }
                        }else{
                            ToastUtil.initToast(baseResponse.getMsg());
                            if (getView()!=null){
                                getView().queryClassroomTestQuestionListFailed();
                            }
                        }
                    }
                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        ToastUtil.initToast(e.getMessage());
                        if (getView()!=null){
                            getView().queryClassroomTestQuestionListFailed();
                        }
                    }
                });
    }

}
